var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp() 

Page({
  data: {
    id: 0, 
    // lat1,
    info: {},
    imgUrl: api.imageUrl, // 图片url
    "image1" : "",  // 上传'图片1',
    "image2" : "",  // 上传'图片2',
    "image3" : "",  // 上传'图片3',
    isFocus: false, //是否获取文本框的焦点
    isShowTextarea: false, //是否显示文本框
    result:'',//巡检结果
  },
  onLoad: function (options) {
    if ('id' in options){
      this.setData({
        id: options.id,
        ticket_num:options.ticket_num,
      })
    }
    if ('station_id' in options){
      this.setData({
        station_id: options.station_id
      })
    }


   this.getInfo()
  },
  //##input 输入##
  input_handle(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  getInfo() { // 获取详情信息
    var params = {
      id: this.data.id,
      station_id:this.data.station_id,
      ticket_num:this.data.ticket_num,
    }
    console.log(this.data.ticket_num+'==========')
    var that=this;
    api.$get(api.pollingManage.info, params).then((res) => { 
      that.setData({
        info: res.data,
        image1:res.data.info.image1,
        image2:res.data.info.image2,
        image3:res.data.info.image3,
      }) 
      console.log()
    })
  },
  upPicsHandle(e) { // ##图片组件传出来的数据##
    var { pics, key } = e.detail
    console.log(pics, key)
    this.setData({
      [key]: pics.length>0?pics[0]:[]
    })
   
  },
  updateHandle() { // 执行巡检任务
    var that = this
    // var lat2 = that.data.info.company.LATITUDE
    // var lng2 = that.data.info.company.LONGITUDE

    var params = {
      id: this.data.id
    }
    // if (Number(juli(lat1, lng1, lat2, lng2)) < 5) {
      api.$post(api.pollingManage.update, params).then((res) => {
        $.toast('巡检成功')
        this.getInfo()
      })
    // } else {
    //   $.toast('不在巡检范围内')
    // }
    
  },

   toPollingManageInspectionRecord(){ // 去继续巡检
    this.toPollingManageInspection();
  },
  async toPollingManageInspection(){//去巡检
    var latitude=this.data.info.company!=null?this.data.info.company.LATITUDE :'';
    var longitude=this.data.info.company!=null?this.data.info.company.LONGITUDE :'';
    var distance= await app.getDistance(parseFloat(latitude),parseFloat(longitude));
    // var distance=0.1;
    console.log("距离",distance)
    if(typeof(distance)=='number'){
      if(distance<=5 || wx.getStorageSync('user_id')==1){//距离小于5km 允许跳转
        if(this.data.info.info.equip==null) 
          $.linkurl(`/pages/pollingManage/pollingManageInspectionRecord/pollingManageInspectionRecord?ticket_num=${this.data.info.info.ticket_num}&company=${this.data.info.company.NAMEFIRM}&factory=${this.data.info.company.NAME}&id=${this.data.id}&info=${JSON.stringify(this.data.info)}`)
        else
          $.linkurl(`/pages/pollingManage/pollingManageInspectionRecord/pollingManageInspectionRecord?ticket_num=${this.data.info.info.ticket_num}&company=${this.data.info.company.NAMEFIRM}&factory=${this.data.info.company.NAME}&id=${this.data.id}&equip=${this.data.info.info.equip}&info=${JSON.stringify(this.data.info)}&type=1`)
      }else if(distance>5){//距离较远  不允许跳转
         $.toast('您当前位置距目标位置较远,无法开始计划任务')
      }else{//数据为 NaN
        if(this.data.info.info.equip==null) 
        $.linkurl(`/pages/pollingManage/pollingManageInspectionRecord/pollingManageInspectionRecord?ticket_num=${this.data.info.info.ticket_num}&company=${this.data.info.company.NAMEFIRM}&factory=${this.data.info.company.NAME}&id=${this.data.id}&info=${JSON.stringify(this.data.info)}`)
      else
        $.linkurl(`/pages/pollingManage/pollingManageInspectionRecord/pollingManageInspectionRecord?ticket_num=${this.data.info.info.ticket_num}&company=${this.data.info.company.NAMEFIRM}&factory=${this.data.info.company.NAME}&id=${this.data.id}&equip=${this.data.info.info.equip}&info=${JSON.stringify(this.data.info)}&type=1`)
      }
    }else{//未设置场站的位置  可以跳转
      if(this.data.info.info.equip==null) 
        $.linkurl(`/pages/pollingManage/pollingManageInspectionRecord/pollingManageInspectionRecord?ticket_num=${this.data.info.info.ticket_num}&company=${this.data.info.company.NAMEFIRM}&factory=${this.data.info.company.NAME}&id=${this.data.id}&info=${JSON.stringify(this.data.info)}`)
      else
        $.linkurl(`/pages/pollingManage/pollingManageInspectionRecord/pollingManageInspectionRecord?ticket_num=${this.data.info.info.ticket_num}&company=${this.data.info.company.NAMEFIRM}&factory=${this.data.info.company.NAME}&id=${this.data.id}&equip=${this.data.info.info.equip}&info=${JSON.stringify(this.data.info)}&type=1`)
    }
    

  },
  openMapHandle(){ // 打卡地图
    if($.isNull(this.data.info.company.LATITUDE) || $.isNull(this.data.info.company.LONGITUDE)){
      $.toast("对不起,该厂站未提供经纬度信息,无法开启地图服务")
      return
    }
      wx.openLocation({
        latitude: this.data.info.company.LATITUDE,
        longitude: this.data.info.company.LONGITUDE,
        address: this.data.info.company.ADDRESSPLACE,
        scale: 18
      })
    

  },

  callHandle(e){
    var { phone } = $.detail(e)
    $.call(phone)
  },
  toFocusHandle() { //解决textarea placeholder 层级过高的bug 微信好坑 
    this.setData({
      isShowTextarea: true
    })
    setTimeout(() => {
      this.setData({
        isFocus: true
      })
    }, 100)
  },
})

